*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


******* CONVENTIONAL TWFE

foreach d in "emp" {

* Load data at the czone-state-industry level
use "`datadir'`s'cbp_county_state_20ind_sample.dta", clear

*Remove DC (starts in 2004)
drop if czone==11304 & state==11

*Keep restaurant industry
keep if wagerank90==1

* Generating variables 

gen mw=max_mw
gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)

**Generate YEAR DUMMIES 
qui tab year, gen(yeard)

 *Dummies region-year
qui tab region, gen(regiond)
gen regionyear=region*10000+year
qui tab regionyear, gen(regionyeard)

*State dummies
qui tab state, gen(stated)
gen trend=year-1989
forvalues z = 1/48 {
gen statedtrend`z'=stated`z'*trend
}

*Czone-year fixed effects
egen czoneyear=group(czone year)

**************generating conditions and controls*************
local window1 "year<=2016"

* Specify clustering

local options "vce(cluster state)"


* Specify outcome/regressor/instrument
local xvar "lmw"

* Specify controls
local year_controls yeard*
local regionyear_controls regionyeard*
local statetrend_controls statedtrend*

ren cty_fips county
merge m:1 county using "`countydir'`s'countytypes.dta"
drop _merge

/* Regression specs */

local yvar "l`d'"
local county_controls "ltot`d'm lworkagepop"


foreach j in "1" "2" "3" "4" {
eststo m`j'_`d': reghdfe  `yvar' `xvar' `county_controls'  if  type`j'==1 & (`window1') , `options' absorb(county year)

}

}

****** PAIR-PERIOD FIXED EFFECTS

foreach u in "county" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear


***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************


gen lmw=log(mw)


foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm


********* MERGE PAIRS ********************
drop state
merge m:1 countypair_id county using "`countydir'`s'countypairtypes.dta"

drop _merge


gen state=int(county/1000)
bys countypair_id: egen state1=min(state)
bys countypair_id: egen state2=max(state)


**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'

egen bordersegment = group(state1 state2)
egen pairperiod=group(`u'pair `period')

*USE DATA UP TO 2006
drop if `period'>2016

*Merge czones
ren county cty_fips
merge m:1 cty_fips using "`czonedir'`s'cty_czone_state_boundaries.dta"
keep if _merge==3
drop _merge
ren cty_fips county 
egen czonestate=group(czone state)


*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"

*Specifications

forval j=1/4 {

foreach h in "emp" {

*local controls "`popvar' "
local controls "`tot`h'var' `popvar'"
local entityfe "i.`u'"

eststo n`h'_`j': reghdfe ``h'var' `mwvar' `controls' if type`j'==1, `options'

}
}


}

/* Print results */

tempfile specs

#delimit ;
esttab m*  using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Conventional TWFE")
	order(`xvar' `county_controls')
	nonotes
	label
	stats(N, labels("N") fmt(0))	
	replace;
#delimit cr

#delimit ;
esttab n* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Pair approach estimation for county-pair subsamples")
	order(`mwvar' `controls')
	nonotes
	label
	stats(N, labels("N") fmt(0))		
	append;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(Table9)

